package com.stm.toolsTest.service.impl;

import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.stm.common.annotation.DataScope;
import com.stm.common.core.text.Convert;
import com.stm.common.exception.BusinessException;
import com.stm.common.utils.DateUtils;
import com.stm.toolLedger.domain.KfTools;
import com.stm.toolLedger.mapper.KfToolsMapper;
import com.stm.toolsTest.domain.KfToolsTestApply;
import com.stm.toolsTest.domain.KfToolsTestReport;
import com.stm.toolsTest.domain.KfToolsTestReportDetail;
import com.stm.toolsTest.domain.KfToolsTestReportTestValue;
import com.stm.toolsTest.mapper.KfToolsTestApplyMapper;
import com.stm.toolsTest.mapper.KfToolsTestReportDetailMapper;
import com.stm.toolsTest.mapper.KfToolsTestReportMapper;
import com.stm.toolsTest.mapper.KfToolsTestReportTestValueMapper;
import com.stm.toolsTest.service.KfToolsTestReportService;

/**
 * 
 * @Description 试验报告impl
 * @CreateTime 2019年4月4日 上午8:43:59
 * @CreateBy 林龙
 */
@Service
public class KfToolsTestReportServiceImpl implements KfToolsTestReportService
{
	@Autowired
    private KfToolsTestReportMapper kfToolsTestReportMapper;
	@Autowired
	private KfToolsTestReportDetailMapper kfToolsTestReportDetailMapper;
	@Autowired
	private KfToolsTestReportTestValueMapper kfToolsTestReportTestValueMapper;
	@Autowired
	private KfToolsMapper kfToolsMapper;
    @Autowired
    private KfToolsTestApplyMapper kfToolsTestApplyMapper;

    /**
     * 根据条件分页查询数据
     * 
     * @param kfTools 信息
     * @return list集合信息
     */
    @Override
    @DataScope(tableAlias = "b2")
    public List<KfToolsTestReport> selectAllPageList(KfToolsTestReport kfToolsTestReport){
        return kfToolsTestReportMapper.selectAllPageList(kfToolsTestReport);
    }

    /**
     * 根据id查询信息
     * 
     * @param id 
     * @return 对象信息
     */
    @Override
    public KfToolsTestReport selectByPrimaryKey(String id){
        return kfToolsTestReportMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增保存信息
     * 
     * @param KfToolsTestReport RFID信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insert(KfToolsTestReport kfToolsTestReport){
    	UUID uuid = UUID.randomUUID();
    	kfToolsTestReport.setId(uuid.toString());
    	kfToolsTestReport.setCreateTime(DateUtils.getNowDate());
    	kfToolsTestReport.setStatus("1");
    	
    	KfToolsTestApply kfToolsTestApply=new KfToolsTestApply();
    	kfToolsTestApply.setStatus("3");
    	kfToolsTestApply.setId(kfToolsTestReport.getApplyId());
    	kfToolsTestApplyMapper.updateByPrimaryKeySelective(kfToolsTestApply);//修改委托单状态
    	
    	
    	for (KfToolsTestReportDetail kfToolsTestReportDetail : kfToolsTestReport.getKfToolsTestReportDetailList()) { //新增报告单明细
    		
    		UUID uuid1 = UUID.randomUUID();
    		kfToolsTestReportDetail.setId(uuid1.toString());
    		kfToolsTestReportDetail.setReportId(uuid.toString());
    		kfToolsTestReportDetail.setCreateBy(kfToolsTestReport.getCreateBy());
    		kfToolsTestReportDetail.setCreateTime(DateUtils.getNowDate());
    		
    		
    		KfTools kfTools=kfToolsMapper.selectByPrimaryKeyWithName(kfToolsTestReportDetail.getToolsId());
    		KfTools kfToolsnew=new KfTools();
    		Calendar cal = Calendar.getInstance();
			cal.setTime(kfTools.getTestDate());//设置起时间
			cal.add(Calendar.DATE, -1);
			
			kfToolsnew.setNextTestDate(cal.getTime());
			kfToolsnew.setId(kfToolsTestReportDetail.getToolsId());
			kfToolsMapper.updateByPrimaryKeySelective(kfToolsnew);//修改工具下次试验日期
    		
    		
    		for (KfToolsTestReportTestValue kfToolsTestReportTestValue : kfToolsTestReportDetail.getKfToolsTestReportTestValueList()) {
    			UUID uuid2 = UUID.randomUUID();
    			kfToolsTestReportTestValue.setId(uuid2.toString());
    			kfToolsTestReportTestValue.setReportId(uuid.toString());
    			kfToolsTestReportTestValue.setReportDetailId(uuid1.toString());
    			kfToolsTestReportTestValue.setCreateBy(kfToolsTestReport.getCreateBy());
    			kfToolsTestReportTestValue.setCreateTime(DateUtils.getNowDate());
    			
    			
    			kfToolsTestReportTestValueMapper.insertSelective(kfToolsTestReportTestValue); //新增试验值
			}
    		
    		
    		kfToolsTestReportDetailMapper.insertSelective(kfToolsTestReportDetail); //新增报告明细
		}

    	
    	return kfToolsTestReportMapper.insertSelective(kfToolsTestReport); //新增报告
    }

	/**
     * 修改保存信息
     * 
     * @param KfToolsTestReport RFID信息
     * @return 结果
     */
    @Override
    @Transactional
    public int update(KfToolsTestReport kfToolsTestReport){
    	
    	kfToolsTestReport.setUpdateTime(DateUtils.getNowDate());
    	
    	String[] kfToolsTestReportIds = Convert.toStringArray(kfToolsTestReport.getId());
    	
    	kfToolsTestReportDetailMapper.deleteByPrimary(kfToolsTestReportIds); //删除明细表
         
        kfToolsTestReportTestValueMapper.deleteByPrimary(kfToolsTestReportIds); //删除实验值
    	
    	for (KfToolsTestReportDetail kfToolsTestReportDetail : kfToolsTestReport.getKfToolsTestReportDetailList()) { //新增报告单明细
    		
    		UUID uuid1 = UUID.randomUUID();
    		kfToolsTestReportDetail.setId(uuid1.toString());
    		kfToolsTestReportDetail.setReportId(kfToolsTestReport.getId());
    		kfToolsTestReportDetail.setCreateBy(kfToolsTestReport.getCreateBy());
    		kfToolsTestReportDetail.setCreateTime(DateUtils.getNowDate());
    		
    		
    		KfTools kfTools=kfToolsMapper.selectByPrimaryKeyWithName(kfToolsTestReportDetail.getToolsId());
    		KfTools kfToolsnew=new KfTools();
    		Calendar cal = Calendar.getInstance();
			cal.setTime(kfTools.getTestDate());//设置起时间
			cal.add(Calendar.DATE, -1);
			
			kfToolsnew.setNextTestDate(cal.getTime());
			kfToolsnew.setId(kfToolsTestReportDetail.getToolsId());
			kfToolsMapper.updateByPrimaryKeySelective(kfToolsnew);//修改工具下次试验日期
    		
    		for (KfToolsTestReportTestValue kfToolsTestReportTestValue : kfToolsTestReportDetail.getKfToolsTestReportTestValueList()) {
    			UUID uuid2 = UUID.randomUUID();
    			kfToolsTestReportTestValue.setId(uuid2.toString());
    			kfToolsTestReportTestValue.setReportId(kfToolsTestReport.getId());
    			kfToolsTestReportTestValue.setReportDetailId(uuid1.toString());
    			kfToolsTestReportTestValue.setCreateBy(kfToolsTestReport.getCreateBy());
    			kfToolsTestReportTestValue.setCreateTime(DateUtils.getNowDate());
    			
    			
    			kfToolsTestReportTestValueMapper.insertSelective(kfToolsTestReportTestValue); //新增试验值
			}
    		
    		
    		kfToolsTestReportDetailMapper.insertSelective(kfToolsTestReportDetail); //新增报告明细
		}
    	return  kfToolsTestReportMapper.updateByPrimaryKeySelective(kfToolsTestReport);
    }
    
    /**
     * 批量删除
     * 
     * @param ids
     * @throws Exception
     */
    @Override
    public int deleteByPrimary(String ids) throws BusinessException
    {
        String[] kfToolsTestReportIds = Convert.toStringArray(ids);
        
        kfToolsTestReportDetailMapper.deleteByPrimary(kfToolsTestReportIds);
        
        kfToolsTestReportTestValueMapper.deleteByPrimary(kfToolsTestReportIds);
        
        return kfToolsTestReportMapper.deleteByPrimary(kfToolsTestReportIds);
    }

	@Override
	public int reviewByPrimary(String ids) {
		
		KfToolsTestReport kfToolsTestReport=kfToolsTestReportMapper.selectByPrimaryKey(ids);
		
		
		List<KfToolsTestReportDetail> kfToolsTestReportDetailList=kfToolsTestReportDetailMapper.selectByPrimaryByMianid(ids);
		for (KfToolsTestReportDetail kfToolsTestReportDetail : kfToolsTestReportDetailList) {
			KfTools kfTools =new KfTools();
			kfTools.setId(kfToolsTestReportDetail.getToolsId());
			kfTools.setStatus("1");
			kfToolsMapper.updateByPrimaryKeySelective(kfTools); //修改库存试验状态
			
		}
		String[] kfToolsTestApplyIds = Convert.toStringArray(ids);
		
		KfToolsTestApply kfToolsTestApply=new KfToolsTestApply();
		kfToolsTestApply.setId(kfToolsTestReport.getApplyId());
		kfToolsTestApply.setStatus("4");
		kfToolsTestApplyMapper.updateByPrimaryKeySelective(kfToolsTestApply); //修改委托单状态
		return kfToolsTestReportMapper.updateByPrimary(kfToolsTestApplyIds);
	}

   


}
